﻿using System;
using System.Web.Configuration;
using System.Data;

public partial class manager_task_NewDefaultAdd : System.Web.UI.Page
{
    public string Html_Dept = "";
    public string Html_Dept_Detail = "";
    public int taskid = 0;
    public string ttitle;
    DBHelper db = new DBHelper(WebConfigurationManager.ConnectionStrings["connString"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        taskid = Convert.ToInt32(Request.QueryString["taskid"]);
        Edit_Id.Value = taskid.ToString();

        if (!IsPostBack)
        {

            GetDept();
            loadData();
            if (taskid != 0)
            {
                loadEditData(taskid);

            }
            else
            {
                int tid = Convert.ToInt32(Request.QueryString["id"].ToString());
                zx_psid.Value = tid.ToString();
                ttitle = (Request.QueryString["title"]);
                bindData(tid);
            }
        }
    }

    private void GetDept()
    {
        int i = 0;
        Html_Dept = "";
        DataTable dt = db.getList("select * from dict_dept where [isUsing]=1 order by id");
        foreach (DataRow dr in dt.Rows)
        {
            if (i == 0)
            {
                Html_Dept += "<li  role=\"presentation\" class=\"active\"><a href=\"#dept_" + dr["id"].ToString() + "\" role=\"tab\" data-toggle=\"tab\">" + dr["mc"].ToString() + "</a></li>";
            }
            else
            {
                Html_Dept += "<li role=\"presentation\"><a href=\"#dept_" + dr["id"].ToString() + "\" role=\"tab\" data-toggle=\"tab\">" + dr["mc"].ToString() + "</a></li>";
            }
            i = 1;
        }
    }

    private void loadData()
    {
        dept_id.Value = "";
        int i = 0;
        DataTable dt = db.getList("select * from dict_dept where [isUsing]=1 order by id");
        foreach (DataRow dr in dt.Rows)
        {
            dept_id.Value = dept_id.Value + "|" + dr["id"].ToString();
            if (i == 0)
            { Html_Dept_Detail += "<div role=\"tabpanel\" class=\"tab-pane active\" id=\"dept_" + dr["id"].ToString() + "\">"; }
            else
            { Html_Dept_Detail += "<div role=\"tabpanel\" class=\"tab-pane\" id=\"dept_" + dr["id"].ToString() + "\">"; }

            Html_Dept_Detail += "<div class=\"box-body\">";
            Html_Dept_Detail += "<table id=\"table_" + dr["id"].ToString() + "\" class=\"table table-bordered table-striped\">";
            Html_Dept_Detail += "<thead>";
            Html_Dept_Detail += "<tr>";
            Html_Dept_Detail += "<th></th>";
            Html_Dept_Detail += "<th class=\"col-sm-7\">名称</th>";
            Html_Dept_Detail += "<th class=\"col-sm-2\">类型</th>";
            Html_Dept_Detail += "<th class=\"col-sm-3\">当前在岗</th>";
            Html_Dept_Detail += "</tr>";
            Html_Dept_Detail += "</thead>";
            Html_Dept_Detail += "<tbody>";
            DataTable dt1 = db.getList("v_dict_dept_position", "*", "dept=" + dr["id"].ToString() + " and type<>1", "bh");
            foreach (DataRow dr1 in dt1.Rows)
            {
                Html_Dept_Detail += "<asp:Repeater runat=\"server\">";
                Html_Dept_Detail += "<ItemTemplate>";
                Html_Dept_Detail += "<tr style=\"cursor:pointer\" onclick=\"SetYsGw('" + dr1["id"].ToString() + "','" + dr1["mc"].ToString() + "|" + dr1["userName"].ToString() + "')\">";
                Html_Dept_Detail += "<td>" + dr1["id"].ToString() + "</td>";
                switch (dr1["type"].ToString())
                {
                    case "1":
                        Html_Dept_Detail += "<td><b>" + dr1["mc"].ToString() + "</b></td>";
                        Html_Dept_Detail += "<td>" + SQ_Eval_Pub.GetPsType(dr1["type"].ToString()) + "</td>";
                        break;
                    case "2":
                        Html_Dept_Detail += "<td style=\"padding-left:30px;\">" + dr1["mc"].ToString() + "</td>";
                        Html_Dept_Detail += "<td style=\"padding-left:15px;\">" + SQ_Eval_Pub.GetPsType(dr1["type"].ToString()) + "</td>";
                        break;
                    case "3":
                        Html_Dept_Detail += "<td style=\"padding-left:50px;\">" + dr1["mc"].ToString() + "</td>";
                        Html_Dept_Detail += "<td style=\"padding-left:25px;\">" + SQ_Eval_Pub.GetPsType(dr1["type"].ToString()) + "</td>";
                        break;
                }
                Html_Dept_Detail += "<td><b>" + dr1["userName"].ToString() + "</b></td>";
                Html_Dept_Detail += "</tr>";
            }
            Html_Dept_Detail += "</tbody>";
            Html_Dept_Detail += "</table>";
            Html_Dept_Detail += "</div>";
            Html_Dept_Detail += "</div>";
            i = 1;
        }
    }

    private void bindData(int tid)
    {
        int type = Convert.ToInt32(db.getList("select type from dict_dept_position where id=" + tid).Rows[0][0].ToString());
        string sql_1 = "select * from [dict_dept_position_evaluate_detail] where relid = (select evalid from dict_dept_position where id = " + tid + ")";
        string sql_2 = "";
        string sql_3 = "";


        string sql_psid2 = "";
        string sql_psid3 = "";

        switch (type)
        {
            case 1:
                html_type_2.Style.Add("display", "none");
                html_type_3.Style.Add("display", "none");
                sql_1 = "select * from [dict_dept_position_evaluate_detail] where relid = (select evalid from dict_dept_position where id = " + tid + ")";
                sql_2 = "select 0 as id,'无' as mc,0 as qz";
                sql_3 = "select 0 as id,'无' as mc,0 as qz";

                sql_psid2 = "select 0 as parentid";
                sql_psid3 = "select 0 as parentid";
                break;
            case 2:
                sql_1 = "select * from [dict_dept_position_evaluate_detail] where relid = (select evalid from dict_dept_position where id = " + tid + ")";
                sql_2 = "select 0 as id,'无' as mc,0 as qz";
                sql_3 = "select id,mc,qz from [dict_dept_position_evaluate_detail] where relid =(select evalid from [dict_dept_position] where id=(select parentid from [dict_dept_position] where id=" + tid + "))";

                sql_psid2 = "select 0 as parentid";
                sql_psid3 = "select parentid from dict_dept_position where id=" + tid;
                html_type_2.Style.Add("display", "none");
                break;
            case 3:
                sql_1 = "select * from [dict_dept_position_evaluate_detail] where relid = (select evalid from dict_dept_position where id = " + tid + ")";
                sql_2 = "select id,mc,qz from [dict_dept_position_evaluate_detail] where relid =(select evalid from [dict_dept_position] where id=(select parentid from [dict_dept_position] where id=" + tid + ")) union select 0 as id,'无' as mc,0 as qz";
                sql_3 = "select id,mc,qz from [dict_dept_position_evaluate_detail] where relid =(select evalid from [dict_dept_position] where id=(select parentid from [dict_dept_position] where id=(select parentid from [dict_dept_position] where id=" + tid + "))) union select 0 as id,'无' as mc,0 as qz";

                sql_psid2 = "select parentid from dict_dept_position where id=" + tid;
                sql_psid3 = "select parentid from dict_dept_position where id=(select parentid from dict_dept_position where id=" + tid + ")";
                break;
        }

        DataTable dt_ps = db.getList(sql_1);
        rptDeApply_1.DataSource = dt_ps;
        rptDeApply_1.DataBind();

        DataTable dt_par = db.getList(sql_2);
        rptDeApply_2.DataSource = dt_par;
        rptDeApply_2.DataBind();

        DataTable dt_dept = db.getList(sql_3);
        rptDeApply_3.DataSource = dt_dept;
        rptDeApply_3.DataBind();

        string t_par_psid = db.getList(sql_psid2).Rows[0][0].ToString();
        string t_dept_psid = db.getList(sql_psid3).Rows[0][0].ToString();
        par_psid.Value = t_par_psid;
        dept_psid.Value = t_dept_psid;

    }


    private void loadEditData(int sid)
    {
        string sql = "select * from v_A_Task where id=" + sid;
        DataTable dt = db.getList(sql);
        if (dt.Rows.Count == 1)
        {





            txt_title.Value = dt.Rows[0]["title"].ToString();
            txt_ms.Value = dt.Rows[0]["detail"].ToString();
            txt_fb_username.Value = dt.Rows[0]["fb_username"].ToString();
            txt_rq_start.Value = dt.Rows[0]["rq_start"].ToString();
            txt_rq_yjs.Value = dt.Rows[0]["rq_yjs"].ToString();
            zx_psid.Value = dt.Rows[0]["zx_psid"].ToString();
            ys_psid.Value = dt.Rows[0]["ys_psid"].ToString();
            eval_ps.Value = dt.Rows[0]["eval_ps"].ToString();
            eval_parps.Value = dt.Rows[0]["eval_parps"].ToString();
            eval_deptps.Value = dt.Rows[0]["eval_deptps"].ToString();
            ys_psid_mc.Value = dt.Rows[0]["ys_psid_mc"].ToString() + "|" + dt.Rows[0]["ys_psid_UserName"].ToString();
            eval_ps_mc.Value = dt.Rows[0]["eval_ps_mc"].ToString();
            eval_parps_mc.Value = dt.Rows[0]["eval_parps_mc"].ToString();
            eval_deptps_mc.Value = dt.Rows[0]["eval_deptps_mc"].ToString();
            bindData(Convert.ToInt32(dt.Rows[0]["zx_psid"].ToString()));
            if (dt.Rows[0]["state"].ToString() == "0")
            {
                ddlIsUsing.SelectedValue = "0";
            }
            else if (dt.Rows[0]["state"].ToString() == "1")
            {
                ddlIsUsing.SelectedValue = "1";
            }
            else
            {
                ddlIsUsing.SelectedValue = "1";
                Button1.Visible = false;
            }

        }


    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        string errmsg = "";

        string title = txt_title.Value;
        string detail = txt_ms.Value;
        string fb_username = txt_fb_username.Value;
        string rq_start = txt_rq_start.Value;
        string rq_yjs = txt_rq_yjs.Value;
        string cz_userbh = Session[AppConfig.SESSION_USER_BH].ToString();
        string tzx_psid = zx_psid.Value;
        string tys_psid = ys_psid.Value;
        string teval_ps = eval_ps.Value;
        string teval_parps = eval_parps.Value;
        string teval_deptps = eval_deptps.Value;
        string tpar_psid = par_psid.Value;
        string tdept_psid = dept_psid.Value;
        string isUsing = ddlIsUsing.SelectedValue;
        int Year = 0;
        int Month = 0;

        if (rq_yjs != "")
        {
            DateTime ss = Convert.ToDateTime(rq_yjs);
            SQ_Eval_Pub.GetYearMonth(ss, ref Year, ref Month);
        }

        if (teval_parps == "0")
        {
            tpar_psid = "0";
        }
        if (teval_deptps == "0")
        {
            tdept_psid = "0";
        }
        string id = Edit_Id.Value;
        string sql = "";
        if (id == "0")
        {
            sql = "insert into A_Task([type],[title],[detail],[rq_start],[rq_yjs],cz_userbh,fb_username,[zx_psid],[ys_psid],[eval_ps],[eval_parps],[eval_deptps],[Year],[Month],[state],ps_par,ps_dept) values";
            sql += "(1,N'" + title + "',N'" + detail + "','" + rq_start + "','" + rq_yjs + "','" + cz_userbh + "','" + fb_username + "','" + tzx_psid + "','" + tys_psid + "','" + teval_ps + "','" + teval_parps + "','" + teval_deptps + "','" + Year + "','" + Month + "','1','" + tpar_psid + "','" + tdept_psid + "')";
        }
        else
        {
            sql = "update A_Task set [title]=N'" + title + "',[detail]=N'" + detail + "',[rq_start]='" + rq_start + "',[rq_yjs]='" + rq_yjs + "',[fb_username]='" + fb_username + "',[zx_psid]='" + tzx_psid + "',[ys_psid]='" + tys_psid + "',[eval_ps]='" + teval_ps + "',[eval_parps]='" + teval_parps + "',[eval_deptps]='" + teval_deptps + "',[state]='" + isUsing + "',ps_par='" + tpar_psid + "',ps_dept='" + tdept_psid + "' where id=" + id + " and ([state]=1 or [state]=0)";
        }
        int i = db.executeNoneQuery(sql, ref errmsg);
        if (i > 0)
        {
            Response.Write(CommonHelper.alertMsgAndRedirect("成功", "NewDefault.aspx"));
        }
        else
        {
            Response.Write(CommonHelper.alertMsg("失败," + errmsg));
        }
    }
}